jQuery(selector).html() 过滤 script tag 的解决方法

  之前用 pjax 做个项目,使用了 .html() 方法将获取到的数据插入 container。但是却发现其会自动过滤 script tag,现找到解决方法 (jquery html() strips out script tags),在此记录一下
  以下是我应用到项目里的部分代码,对 stackoverflow 的答案多进行了一次判断

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
$(document).on("pjax:end", function(event, data) {
var responseDom = $(data.responseText);
if (!$(event.target).filter("script").length) {
responseDom.filter('script').each(function(){
if (this.src) {
var script = document.createElement('script'), i, attrName, attrValue, attrs = this.attributes;
for(i = 0; i < attrs.length; i++) {
attrName = attrs[i].name;
attrValue = attrs[i].value;
script[attrName] = attrValue;
}
event.target.appendChild(script);
} else {
$.globalEval(this.text || this.textContent || this.innerHTML || '');
}
});
}
});